<?php
namespace App\Http\Controllers\Home;
use App\http\Controllers\Controller;

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use Request;

class LoginController extends Controller{
   
    public function login(){
        
        return view('home/login');
    }

    public function login_check(){
        $input=Request::all();
        $user_name=$input['user_name'];
		$user_pwd=md5($input['user_pwd'].'apdream.cn');
        //  echo $user_name.'<br/>';echo $user_pwd;
        //  die;
        //防止sql注入的字符串过滤
	    $user=DB::table('user')->whereRaw("user_name=? AND user_pwd=?",["$user_name","$user_pwd"])->first();
        if($user){
            //设置session
			Session::put('go_login','2');
			Session::save();
			//设置cookie
			Cookie::queue('u_name',$user_name,60);//60分钟
			Cookie::queue('u_time',time(),60);
            // $u_name=Cookie::get('u_name');
            return '<script>alert("登录成功");location.href="/";</script>';exit();
            
            
        }else{
            return '<script>alert("用户名或密码有误");location.href="/login";</script>';
           
            
        }
    }
    public function register(){
        $input=Request::all();
        $user_pwd=md5($input['user_pwd'].'apdream.cn');
        $user_pwd2=md5($input['user_pwd2'].'apdream.cn');
        $data=array(
            'user_name'=>$input['user_name'],
            'user_email'=>$input['user_email'],
            'user_pwd'=>$user_pwd,
        );
        //  echo $user_pwd.'<br/>';echo $user_pwd2;
        //  die;
         if($user_pwd!=$user_pwd2){
             show_msg("两次密码输入不一致","/login");
            
        }
        $res=DB::table('user')->insert($data);
        if($res){ //成功
            show_msg("注册成功","/login");
        }else{
            show_msg("数据执行有误，请重试","/login");
        }
       
       
    }

    //退出
		public function login_out(){
			//清除cookie
			Cookie::queue(Cookie::forget('u_name'));
			Cookie::queue(Cookie::forget('u_time'));
			//清除session
			Session::forget('go_login');
			return '<script>alert("退出成功");location.href="/login";</script>;';
		}
    
}